package com.cn.newetv.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

import com.cn.newetv.content.EtvData;
import com.cn.newetv.po.FavDataBaseDO;

public class FavListDao {
	private ContentResolver contentResolver;
	public void openDB(Context context) {
		try {
			contentResolver = context.getContentResolver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(FavDataBaseDO favDataBaseDO, String filmId, Integer mode) {
		try {
			ContentValues values = new ContentValues();
			values.put(EtvData.FavListDO.FVIDEONAME, favDataBaseDO.getVideoname());
			values.put(EtvData.FavListDO.FVIDELTYPE, favDataBaseDO.getVideotype());
			values.put(EtvData.FavListDO.FVIDEODATA, favDataBaseDO.getVideodata());
			values.put(EtvData.FavListDO.FINTIME, favDataBaseDO.getIntime());
			values.put(EtvData.FavFilmDO.FFILMID, filmId);
			values.put(EtvData.FavFilmDO.FMODE, mode);
			contentResolver.insert(EtvData.FavListDO.WORD_CONTENT_URI, values);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteById(Integer id) {
		try {
			Uri resultUri = ContentUris.withAppendedId(EtvData.FavListDO.WORD_CONTENT_URI, id.longValue());
			contentResolver.delete(resultUri, null, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void deleteByName(String name, Integer mode) {
		try {
			String selection = "videoname=? and mode=?";
			String[] selectionArgs = new String[]{name,""+mode};
			contentResolver.delete(EtvData.FavListDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void deleteByFilmId(String filmId, Integer mode) {
		try {
			String selection = "filmId=? and mode=?";
			String[] selectionArgs = new String[]{filmId,""+mode};
			contentResolver.delete(EtvData.FavListDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public FavDataBaseDO getByName(String name, Integer mode) {
		try {
			String[] projection = new String[]{EtvData.FavListDO.FID,EtvData.FavListDO.FVIDEONAME,EtvData.FavListDO.FVIDELTYPE,EtvData.FavListDO.FVIDEODATA,EtvData.FavListDO.FINTIME};
			String selection = "videoname=? and mode=?";
			String[] selectionArgs = new String[]{name,""+mode};
			Cursor cursor = contentResolver.query(EtvData.FavListDO.DICT_CONTENT_URI, projection, selection, selectionArgs, null);
			if(cursor == null || cursor.getCount() == 0)
				return null;
			cursor.moveToNext();
			FavDataBaseDO favDataBaseDO = new FavDataBaseDO();
			favDataBaseDO.setId(cursor.getInt(0));
			favDataBaseDO.setVideoname(cursor.getString(1));
			favDataBaseDO.setVideotype(cursor.getInt(2));
			favDataBaseDO.setVideodata(cursor.getString(3));
			favDataBaseDO.setIntime(cursor.getString(4));
			return favDataBaseDO;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<FavDataBaseDO> getByFilmId(String filmId, Integer mode) {
		List<FavDataBaseDO> res = new ArrayList<FavDataBaseDO>();
		try {
			String[] projection = new String[]{EtvData.FavListDO.FID,EtvData.FavListDO.FVIDEONAME,EtvData.FavListDO.FVIDELTYPE,EtvData.FavListDO.FVIDEODATA,EtvData.FavListDO.FINTIME};
			String selection = "filmid=? and mode=?";
			String[] selectionArgs = new String[]{filmId,""+mode};
			Cursor cursor = contentResolver.query(EtvData.FavListDO.DICT_CONTENT_URI, projection, selection, selectionArgs, null);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				//res.put(cursor.getString(0), cursor.getString(1));
				FavDataBaseDO favDataBaseDO = new FavDataBaseDO();
				favDataBaseDO.setId(cursor.getInt(0));
				favDataBaseDO.setVideoname(cursor.getString(1));
				favDataBaseDO.setVideotype(cursor.getInt(2));
				favDataBaseDO.setVideodata(cursor.getString(3));
				favDataBaseDO.setIntime(cursor.getString(4));
				res.add(favDataBaseDO);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	public List<FavDataBaseDO> findAll(Integer mode) {
		List<FavDataBaseDO> res = new ArrayList<FavDataBaseDO>();
		try {
			String[] projection = new String[]{EtvData.FavListDO.FID,EtvData.FavListDO.FVIDEONAME,EtvData.FavListDO.FVIDELTYPE,EtvData.FavListDO.FVIDEODATA,EtvData.FavListDO.FINTIME};
			String sortOrder = "intime desc";
			String selection = "mode=?";
			String[] selectionArgs = new String[]{""+mode};
			Cursor cursor = contentResolver.query(EtvData.FavListDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				//res.put(cursor.getString(0), cursor.getString(1));
				FavDataBaseDO favDataBaseDO = new FavDataBaseDO();
				favDataBaseDO.setId(cursor.getInt(0));
				favDataBaseDO.setVideoname(cursor.getString(1));
				favDataBaseDO.setVideotype(cursor.getInt(2));
				favDataBaseDO.setVideodata(cursor.getString(3));
				favDataBaseDO.setIntime(cursor.getString(4));
				res.add(favDataBaseDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}
